/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.controller.bean;

import com.javacommunity.model.dao.CategoryDAO;
import com.javacommunity.model.dao.CommentDAO;
import com.javacommunity.model.dao.PostDAO;
import com.javacommunity.model.dao.PostVotationDAO;
import com.javacommunity.model.dao.impl.CategoryDAOImpl;
import com.javacommunity.model.dao.impl.CommentDAOImpl;
import com.javacommunity.model.dao.impl.PostDAOImpl;
import com.javacommunity.model.dao.impl.PostVotationDAOImpl;
import com.javacommunity.model.entity.Category;
import com.javacommunity.model.entity.Comment;
import com.javacommunity.model.entity.Post;
import com.javacommunity.util.Support;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Cong Pham Dinh
 */
@ManagedBean(name = "postManagement")
@RequestScoped
public class PostManagement {

    private final PostDAO postDAO = PostDAOImpl.getInstance();
    private final CommentDAO commentDAO = CommentDAOImpl.getInstance();
    private final PostVotationDAO postVotationDAO = PostVotationDAOImpl.getInstance();
    private final CategoryDAO categoryDAO = CategoryDAOImpl.getInstance();
    
    private List<Category> categorys = new ArrayList<Category>();

    private Post selectedPost = new Post();
    private Comment selectedComment = new Comment();
    private Category category = new Category();

    
    @ManagedProperty(value="#{param.id}")
    private int categoryId;
    
    public List<Post> getPostByCategory(){
        category = categoryDAO.getCategoryById(getCategoryId());
        return category.getPosts();
    }
    
    public List<Post> getPosts(int param){
        return postDAO.getPosts(param);
    }
    
    public String createPost(ActionEvent evt) {
        String title = this.selectedPost.getTitle();
        String content = this.selectedPost.getContent();
        Category c = this.selectedPost.getCategory();

        Post p = new Post(c, Support.getCurrentUser(), title, content, new Date(), new Date(), 1, true);
        if (postDAO.createPost(p)) {
            return "post-management.jsf?faces-redirect=true";
        } else {
            return "update-post.jsf";
        }
    }

    public String updatePost(ActionEvent evt) {
        Post p = postDAO.getPostById(this.selectedPost.getPostId());
        String title = this.selectedPost.getTitle();
        String content = this.selectedPost.getContent();
        Category c = this.selectedPost.getCategory();
        p.setTitle(title);
        p.setContent(content);
        p.setCategory(c);
        p.setUpdateDate(new Date());
        p.setUser(Support.getCurrentUser());

        if (postDAO.updatePost(p)) {
            this.addMessage("Update thanh cong roi, dmm!");
            return "post-management.jsf?faces-redirect=true";
        } else {
            this.addMessage("Update that cmn bai roi, dmm!");
            return "update-post.jsf";
        }
    }

    public void deleteComment(int commentId){
        if(commentDAO.deleteComment(commentDAO.getCommentById(commentId)))
            this.addMessage("Xoa comment thanh cong!");
        else
            this.addMessage("Xoa comment nhu lz!");
    }
    
    public String subContent(Post post, int number) {
        return (String) post.getContent().subSequence(0, post.getContent().indexOf(" ", number));
    }

    public void enablePost(ActionEvent evt) {
        String msg;
        if (postDAO.restorePost(this.selectedPost)) {
            msg = "Enabled successful post!";
        } else {
            msg = "Enabled failed post!";
        }
        this.addMessage(msg);
    }

    public void disablePost(ActionEvent evt) {
        String msg;
        if (postDAO.removePost(this.selectedPost)) {
            msg = "Disabled successful post !";
        } else {
            msg = "Disabled failed post!";
        }
        this.addMessage(msg);
    }

    public void deletePost(ActionEvent evt) {
        String msg;
        Post p = postDAO.getPostById(this.selectedPost.getPostId());
        if (postDAO.deletePost(p)) {
            msg = "Deleted successful post !";
        } else {
            msg = "Delete this post has failed!";
        }
        this.addMessage(msg);
    }

    public int getVotes(Post p) {
        return postVotationDAO.countVotation(p);
    }

    public String convertDate(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        return df.format(date);
    }

    public void addMessage(String msg) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, "Message!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public List<Category> getCategorys() {
        categorys = categoryDAO.getCategories("all");
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public Post getSelectedPost() {
        return selectedPost;
    }

    public void setSelectedPost(Post selectedPost) {
        this.selectedPost = selectedPost;
    }

    public Comment getSelectedComment() {
        return selectedComment;
    }

    public void setSelectedComment(Comment selectedComment) {
        this.selectedComment = selectedComment;
    }

    /**
     * @return the category
     */
    public Category getCategory() {
        return category;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(Category category) {
        this.category = category;
    }

    /**
     * @return the categoryId
     */
    public int getCategoryId() {
        return categoryId;
    }

    /**
     * @param categoryId the categoryId to set
     */
    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

}
